package demo.android.myapplication.demos.animation

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Camera
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import androidx.core.graphics.withSave
import cn.android.basic.*
import demo.android.myapplication.R

/**
 * <br>createBy Gw
 * <br>createTime: 2021/7/9 17:47
 */
class FoldAnimationView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val source = decodeBitmap(R.mipmap.ic_launcher_copy, 150.dp.int)
    private val camera = Camera().apply {
        setLocation(0f, 0f, -10f)
    }
    private val rect = RectF()

    var rotate = 0f
        set(value) {
            field = value
            invalidate()
        }
    var foldAngle = 0f
        set(value) {
            field = value
            invalidate()
        }
    var otherFoldAngle = 0f
        set(value) {
            field = value
            invalidate()
        }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        val animator1 = ObjectAnimator.ofFloat(this, "foldAngle", 45f)
            .apply {
                duration = 500
            }
        val animator2 = ObjectAnimator.ofFloat(this, "rotate", 270f)
            .apply {
                duration = 1000
            }
        val animator3 = ObjectAnimator.ofFloat(this, "otherFoldAngle", 45f)
            .apply {
                duration = 1000
            }

        val set = AnimatorSet()
        set.startDelay = 1000
        set.playSequentially(animator1, animator2, animator3)
        set.start()
    }


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.withSave {
            canvas.rotate(-rotate, centerX, centerY)
            rect.set(0f, 0f, width.float, centerY)
            camera.withSave {
                canvas.translate(centerX, centerY)
                camera.rotateX(-otherFoldAngle)
                camera.applyToCanvas(canvas)
                canvas.translate(-centerX, -centerY)
            }
            canvas.clipRect(rect)
            canvas.rotate(rotate, centerX, centerY)
            canvas.drawBitmap(source, centerX - source.centerX, centerY - source.centerY, paint)
        }

        canvas.withSave {
            canvas.rotate(-rotate, centerX, centerY)
            rect.set(0f, centerY, width.float, height.float)
            camera.withSave {
                canvas.translate(centerX, centerY)
                camera.rotateX(foldAngle)
                camera.applyToCanvas(canvas)
                canvas.translate(-centerX, -centerY)
            }
            canvas.clipRect(rect)
            canvas.rotate(rotate, centerX, centerY)
            canvas.drawBitmap(source, centerX - source.centerX, centerY - source.centerY, paint)
        }
    }
}